﻿using System;
using SilverChart.Core.Service;

namespace SilverChart.Core.Configuration.Converter {
    [ServiceProvider(LayzeCreation = true, ServiceName = "Default Boolean configuration converter", Singleton = true)]
    public class BooleanConverter : ConfigurationConverter<Boolean> {

        #region ConfigurationConverter<bool> Members

        public bool Convert(String itemValue, out bool value) {
            if (itemValue == null || !Boolean.TryParse(itemValue, out value)) {
                if (itemValue == "1" || itemValue.ToUpper() == "Y" || itemValue.ToUpper() == "YES" || itemValue.ToUpper() == "T") {
                    value = true;
                    return true;
                }
                else if (itemValue == "0" || itemValue.ToUpper() == "N" || itemValue.ToUpper() == "NO" || itemValue.ToUpper() == "F") {
                    value = false;
                    return true;
                }

                value = false;
                return false;
            }
            return true;
        }

        #endregion
    }
}
